package com.todo.strategy.offline.application.handler.statusChange;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.todo.common.common.entity.response.BizResponse;
import com.todo.common.utils.TodoCommonUtils;
import com.todo.strategy.common.enums.StrategyStatusEnum;
import com.todo.strategy.offline.application.entity.context.StrategyStatusContext;
import com.todo.strategy.offline.application.remote.StrategyPipelineRemoteService;
import com.todo.strategy.offline.infrastructure.entity.domain.PlatformStrategyDO;
import com.todo.strategy.offline.infrastructure.repository.PlatformStrategyRepository;
import com.todo.strategy.pipeline.common.enums.OperateActionEnum;
import com.todo.strategy.pipeline.common.enums.StrategyActionEnum;
import com.todo.strategy.pipeline.service.api.request.OfflineToOnlineRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * @author azhebuxing
 * @date 2024/12/12 19:37
 * @description
 */
@Slf4j
@Component
public class DisableHandler extends AbstractStrategyStatusHandler {

    @Resource
    private PlatformStrategyRepository platformStrategyRepository;
    @Resource
    private StrategyPipelineRemoteService strategyPipelineRemoteService;


    @Override
    protected Boolean doStatusHandle(StrategyStatusContext context) {
        Long strategyId = context.getStrategyId();
        PlatformStrategyDO strategyDO = platformStrategyRepository.queryPlatformStrategyById(strategyId);
        if (strategyDO == null || strategyDO.getBaseStrategyInfo() == null) {
            log.error("DisableHandler#doStatusHandle 平台攻略发布状态处理完成，数据为空，默认执行成功 context:【{}】", JSONObject.toJSONString(context));
            return true;
        }
        if (!strategyDO.getBaseStrategyInfo().getStatus().equals(context.getCurrentStatus().getType())) {
            log.error("DisableHandler#doStatusHandle 平台攻略当前数据状态与发送消息状态不一致，无法处理当前状态下的数据情况 context:【{}】，当前信息 :【{}】", JSONObject.toJSONString(context), JSONObject.toJSONString(strategyDO));
            return true;
        }
        //发送消息给piple 同步线上数据
        OfflineToOnlineRequest disableRequest = new OfflineToOnlineRequest();
        disableRequest.setBusinessId(strategyId);
        //唯一标识 攻略id+更新时间hash 注：后续如果新增版本概念可以通过id+version确认
        disableRequest.setUniKey(TodoCommonUtils.buildStriHashKey(strategyId, strategyDO.getUpdateTime()));
        disableRequest.setStrategyActionEnum(StrategyActionEnum.PLATFORM_STRATEGY);
        disableRequest.setOperateActionEnum(OperateActionEnum.DISABLE);
        disableRequest.setOriginData(JSON.toJSONString(strategyDO));
        BizResponse<Boolean> bizResponse = strategyPipelineRemoteService.offlineToOnlineAsync(disableRequest);
        if (bizResponse == null || !bizResponse.isSuccess() || !bizResponse.getData()) {
            log.error("DisableHandler#doStatusHandle 平台攻略发布状态处理失败，数据同步失败 context:{}", JSONObject.toJSONString(context));
        }
        return true;
    }

    @Override
    protected boolean match(StrategyStatusContext context) {
        if (context == null || context.getStrategyId() == null || context.getCurrentStatus() == null) {
            log.error("DisableHandler#match 平台攻略前置数据匹配失败，数据缺失或不存在 context:【{}】", JSONObject.toJSONString(context));
            return false;
        }
        return true;
    }

    @Override
    protected StrategyStatusEnum doGetDealStatus() {
        return StrategyStatusEnum.DISABLE;
    }
}
